package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import orm.ChildrenBehaviorPurpose;
import orm.CollectivityMonthArrange;
import orm.CollectivityWeekArrange;
import orm.CollectivityWeekTimeBucket;
import util.DBHelper;

public class CMonthArrangeDAO {
	
	
	public void insertCMonthArrange(Statement statement, 
			ArrayList<CollectivityMonthArrange> cmaArr, String cMonthArrangeID) throws SQLException {
		CollectivityMonthArrange cma = null;
		for (int i = 0; i < cmaArr.size(); i++) {
			cma = cmaArr.get(i);
			statement.execute("insert into collectivitymontharrange values('" + cMonthArrangeID + "'," +
					cma.getWeekID() + ", '" + cma.getCWeekArrangeID() + "', '" + cma.getRemark() + "')");
		}
	}
	
	public void updateCMonthArrange(Statement statement,
			ArrayList<CollectivityMonthArrange> cmaArr, String cMonthArrangeID) throws SQLException {
		CollectivityMonthArrange cma = null;
		for (int i = 0; i < cmaArr.size(); i++) {
			cma = cmaArr.get(i);

			StringBuffer sqlStr = new StringBuffer();
			sqlStr.append("update collectivitymontharrange set CWeekArrangeID = '" );
			sqlStr.append(cma.getCWeekArrangeID() + "', Remark = '" + cma.getRemark());
			sqlStr.append("' where cMonthArrangeID = '" + cma.getCMonthArrangeID() + "' and WeekID = " + cma.getWeekID() + "");
			statement.execute(sqlStr.toString());
		}
	}
	
	public void deleteCMonthArrange(Statement statement, String cMonthArrangeID) throws SQLException {
		statement.execute("delete from collectivitymontharrange where CMonthArrangeID='" + cMonthArrangeID + "'");
	}
	
	public ArrayList<CollectivityMonthArrange> getcmaBycmaID(String cMonthArrangeID) throws SQLException {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		ArrayList<CollectivityMonthArrange> al = new ArrayList<CollectivityMonthArrange>();
		CollectivityMonthArrange cma = null;
		conn = DBHelper.getConnection();
		stmt = conn.createStatement();
		rs = stmt.executeQuery("select * from collectivitymontharrange where CMonthArrangeID='" + cMonthArrangeID + "'");
		
		while (rs.next()) {
			cma = new CollectivityMonthArrange();
			int i = 1;
			cma.setCMonthArrangeID(rs.getString(i++));
			cma.setWeekID(rs.getInt(i++));
			cma.setCWeekArrangeID(rs.getString(i++));
			cma.setRemark(rs.getString(i++));
			al.add(cma);
		}
		rs.close();
		stmt.close();
		conn.close();
		
		return al;
	}
}
